#!/bin/bash
baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)
# 判断git是否使用gnutls编译,如果是,提醒用户重新编译
git_core_dir=`find /usr/lib* -type d -name git-core`
confirm_rebuild_git=NO
rebuild_git=
if [ "X${git_core_dir}" == "X" ];then
    # 判断能否找到,如果找不到直接重新安装git,目前没想到什么情况下会不存在这个目录,git未安装?
    echo "未找到git-core目录,重新安装git"
    confirm_rebuild_git=YES
else
    if [ -f "${git_core_dir}/git-http-fetch" ];then
        is_gnutls=`ldd ${git_core_dir}/git-http-fetch |grep libcurl-gnutls.so`
        if [ "X${is_gnutls}" != "X" ];then
	    echo ' '
	    echo '========================================================================='
            echo """检测到git使用gnutls编译,如果使用http/https协议下载大仓库代码,大概率出现
error: RPC failed; curl 56 GnuTLS recv error (-110)错误,建议重新安装git
=========================================================================

"""
            confirm_rebuild_git=YES
        fi
    else
        echo "未找到git-http-fetch,您的git不支持http/https协议下载git代码,建议重新安装git"
        confirm_rebuild_git=YES
    fi

fi
if [ "${confirm_rebuild_git}" == "YES" ];then
    read -p "请确认是否重新安装git (Y/N):" rebuild_git
fi
# 输入转小写
rebuild_git=`echo ${rebuild_git}|tr '[A-Z]' '[a-z]'`
if [ "${rebuild_git}" == "y" -o "${rebuild_git}" == "yes" ];then
    echo "即将重新安装git"
else
    exit
fi
# 判断是否特定网络环境
unset http_proxy
unset https_proxy
is_huawei_tmp=`curl -I -m 10 -o /dev/null -s -w %{http_code} w3.huawei.com`
if [ "${is_huawei_tmp}" == "200" ];then
    is_huawei=true
else
    is_huawei=false
fi
# 如果是HW内网,直接下载deb安装包安装
if [ "${is_huawei}" == "true" ];then
    sudo apt-get install build-essential fakeroot dpkg-dev -y
    sudo apt-get build-dep git -y
    sudo apt-get install libcurl4-openssl-dev -y
    
    mkdir /tmp/source-git
    cd /tmp/source-git
    apt-get source git 
    cd git-2.*.*/
    sed -i -- 's/libcurl4-gnutls-dev/libcurl4-openssl-dev/' ./debian/control
    sed -i -- '/TEST\s*=\s*test/d' ./debian/rules
    dpkg-buildpackage -rfakeroot -b -uc -us
    sudo dpkg -i ../git_*ubuntu*.deb
    exit
    # 以下地址已经失效
    git_deb_url=http://10.62.193.136:8081/isource/git/hw-2.22.0/ubuntu/64bit/git-huawei_2.22.0_amd64.deb
    wget -O /tmp/git-huawei_2.22.0_amd64.deb ${git_deb_url}
    sudo dpkg -i /tmp/git-huawei_2.22.0_amd64.deb
    exit
fi

source ${baseDirForScriptSelf}/source.sh
unset http_proxy
unset https_proxy
# 设置Ubuntu的安装源,预置阿里云源,如需要修改,请编辑apt_get.sh; 如不需要设置,请注释
bash ${baseDirForScriptSelf}/apt_get.sh
if [ $? -gt 0 ];then
    exit 1
fi
sudo apt update
# 卸载gnutls,卸载git
sudo apt remove --purge libcurl4-gnutls-dev -y || true
sudo apt remove --purge git -y || true
# 安装相关依赖
sudo apt install dh-autoreconf curl unzip libcurl4-openssl-dev libexpat1-dev make gettext libz-dev libssl-dev libghc-zlib-dev  tcl-dev asciidoc  build-essential autoconf -y
# 清理apt
sudo apt-get autoremove -y
sudo apt-get autoclean  -y
# 设置代理
source ${baseDirForScriptSelf}/proxy.sh
if [ ! -d ${work_dir} ];then
	mkdir ${work_dir}
fi
cd ${work_dir}
# 从gitee获取最新版本zip包
git_tarball_url="https://gitee.com$(curl -s -k https://gitee.com/mirrors/git/tags|grep -o 'href="/mirrors/git/repository/archive/.*"'|head -n1 |awk -F '"' '{print $2}' | tr -d '\n')"
echo "DOWNLOADING FROM: ${git_tarball_url}"
curl -s -k -L --retry 5 "${git_tarball_url}" --output "git-source.zip"
sudo unzip -d "${work_dir}" git-source.zip
cd "${work_dir}/git"
# 源码编译安装
sudo make configure
sudo ./configure --prefix=/usr --with-openssl
sudo make
sudo make install
bash -c 'echo "$(which git) ($(git --version))"'
